package com.ep.webviewdemo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

/**
 * Created by Administrator on 2017/8/15.
 */

public class WebViewActivity extends AppCompatActivity {
    public final static String URL="url";
    private WebView mWebview;
    private ProgressBar mProgressBar;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        mWebview= (WebView) findViewById(R.id.webview);
        mProgressBar= (ProgressBar) findViewById(R.id.progressBar);

        String content = getIntent().getStringExtra(URL);
        if (TextUtils.isEmpty(content)){
            throw new NullPointerException("content can not be null");
        }

        setWebVew(mWebview);
        setWebListener();
        if (content.startsWith("<")){
            String encoding = "UTF-8";
            String mimeType = "text/html";
            mWebview.loadDataWithBaseURL("about:blank", content, mimeType, encoding, null);
        }else {
            mWebview.loadUrl(content);
        }
    }


    private void setWebListener() {
        mWebview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        mWebview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    mProgressBar.setVisibility(View.GONE);//加载完网页进度条消失
                } else {
                    mProgressBar.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                    mProgressBar.setProgress(newProgress);//设置进度值
                }

            }
        });
    }

    private void setWebVew(WebView webView) {
        WebSettings settings = webView.getSettings();//获得浏览器设置
        settings.setJavaScriptEnabled(true);  //支持js

        settings.setUseWideViewPort(false);  //将图片调整到适合webview的大小

        settings.setSupportZoom(true);  //支持缩放

        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN); //支持内容重      新布局

        settings.supportMultipleWindows();  //多窗口

        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);  //关闭webview中缓存

        settings.setAllowFileAccess(true);  //设置可以访问文件

        settings.setNeedInitialFocus(true); //当webview调用requestFocus时为webview设置节点

        settings.setBuiltInZoomControls(false); //设置支持缩放

        settings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口

        settings.setLoadWithOverviewMode(false); // 缩放至屏幕的大小

        settings.setLoadsImagesAutomatically(true);  //支持自动加载图片

    }
}
